package projectisd.server;

import projectisd.client.Afmelden;

import java.util.List;

import javax.jdo.PersistenceManager;
import projectisd.server.PmfSingleton;

public class AfmeldenDAOJdo implements AfmeldenDAO {

	public void addAfmelden(Afmelden afmelden) {
		PersistenceManager pm = PmfSingleton.get().getPersistenceManager();
		try {
			pm.makePersistent(afmelden);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Afmelden> listAfmeldingen() {
		PersistenceManager pm = PmfSingleton.get().getPersistenceManager();
		String query = "select from " + Afmelden.class.getName();
		return (List<Afmelden>) pm.newQuery(query).execute();
	}

	public void removeAfmelden(Afmelden afmelden) {
		PersistenceManager pm =PmfSingleton.get().getPersistenceManager();
		try {
			pm.currentTransaction().begin();

			// We don't have a reference to the selected Object.
			// So we have to look it up first,
			afmelden = pm.getObjectById(Afmelden.class, afmelden.getAfmeldNr());
			pm.deletePersistent(afmelden);

			pm.currentTransaction().commit();
		} catch (Exception ex) {
			pm.currentTransaction().rollback();
			throw new RuntimeException(ex);
		} finally {
			pm.close();
		}
	}

	public void updateAfmelden(Afmelden afmelden) {
		PersistenceManager pm = PmfSingleton.get().getPersistenceManager();
		
		String reden = afmelden.getReden();
		String studentnummer = afmelden.getStudentnummer();
		Long lesUurNr = afmelden.getLesUurNr();
		//Long studNr = afmelden.getStudNr();

		try {
			pm.currentTransaction().begin();
			// We don't have a reference to the selected Product.
			// So we have to look it up first,
			afmelden = pm.getObjectById(Afmelden.class, afmelden.getAfmeldNr());
			afmelden.setReden(reden);
			afmelden.setStudentnummer(studentnummer);
			afmelden.setLesUurNr(lesUurNr);
			//afmelden.setStudNr(studNr);

			pm.makePersistent(afmelden);
			pm.currentTransaction().commit();
		} catch (Exception ex) {
			pm.currentTransaction().rollback();
			throw new RuntimeException(ex);
		} finally {
			pm.close();
		}
	}

}
